package com.guoguo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.guoguo.entity.AcAlbumImage;
import com.guoguo.entity.Images;
import com.guoguo.entity.UserExt;
import com.guoguo.enums.AliOssImageEnum;
import com.guoguo.mapper.AcAlbumImageMapper;
import com.guoguo.mapper.ImagesMapper;
import com.guoguo.mapper.UserExtMapper;
import com.guoguo.mapper.UsersMapper;
import com.guoguo.service.IUserExtService;
import com.guoguo.service.UploadService;
import com.guoguo.utils.*;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * Created by Administrator on 2017/8/28 0028.
 */

     /*
                           _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                          O\  =  /O
                       ____/`---'\____
                     .'  \\|     |//  `.
                    /  \\|||  :  |||//  \
                   /  _||||| -:- |||||-  \
                   |   | \\\  -  /// |   |
                   | \_|  ''\---/''  |   |
                   \  .-\__  `-`  ___/-. /
                 ___`. .'  /--.--\  `. . __
              ."" '<  `.___\_<|>_/___.'  >'"".
             | | :  `- \`.;`\ _ /`;.`/ - ` : | |
             \  \ `-.   \_ __\ /__ _/   .-` /  /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 佛祖保佑       永无BUG
        */

/**
 * 用户上传图片
 */
@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private ImagesMapper imagesMapper;

    @Autowired
    private AcAlbumImageMapper acAlbumImageMapper;


    @Autowired
    private UserExtMapper iUserExtMapper;

    /**
     * 上传单个文件
     *
     * @param fileNames
     * @return
     */
    @Override
    public List<String> uploadFile(List<String> fileNames) {

        List<String> ossFile = AliOssUtils.upLoadFileList(fileNames);
        return ossFile;
    }

    /**
     * 批量上传文件
     *
     * @param albId
     * @param ossFile
     * @return
     */
    @Override
    public Map<String, String> uploadFiles(String albId, List<String> ossFile) {
        Map<String, String> imageIds = new HashMap<>();
        int i = 1;

//        List<AliOssImageConfig> aliOssImageConfigsList = new ArrayList<>();
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
//        aliOssImageConfigsList.add(aliOssImageConfig);

        for (String s : ossFile) {
            Images images = new Images();
            AcAlbumImage acAlbumImage = new AcAlbumImage();


            images.setImgId(UUIDUtils.getUUID());
            // TODO: 2017/11/20 0020 冗余专辑id
            images.setAlbId(albId);
            //保存原图url
            images.setOriginalUrl(s);
            //保存压缩图
            images.setCompressionUrl(s);
            //保存缩略图
            images.setThumbnailsUrl(s);
            images.setIsFree(1);
            images.setSort(i);
            images.setCommentNum(0);
            images.setFavoriteNum(0);
            images.setShareNum(0);

            images.setCreateTime(new Date());
            //保存image
            imagesMapper.insert(images);

            acAlbumImage.setAlbId(albId);
            acAlbumImage.setImgId(images.getImgId());
            //保存image和album的关联
            acAlbumImageMapper.insert(acAlbumImage);

            Map<String, String> url = AliOssUtils.getImageUrlList(ossFile);//, aliOssImageConfigsList);
//            Map<String, String> url = AliOssUtils.getImageUrlList(ossFile);
            imageIds.put(images.getImgId(), url.get(s));
            i++;
        }
        return imageIds;
    }

    /**
     * 对一个用户生成一个邀请二维码，并将其上传到云上。
     * 返回一个云上的二维码地址
     *
     * @param userId
     * @return
     */
    @Override
    public String uploadQRCode(String userId, String albId) {
        UserExt userExt = iUserExtMapper.findUserExt(userId);
        StringBuffer aliUrl = new StringBuffer();

        if (userExt != null && userExt.getQrcode() != null) {
            aliUrl.append(userExt.getQrcode());
        } else {
            StringBuffer qrCodeContent = new StringBuffer();
            qrCodeContent.append(PropertyUtils.getProperty("api.properties", "api.share.url")).append("?userId=" + userId);
            if (!StringUtils.isEmptyString(albId)) {
                qrCodeContent.append("&albId=").append(albId);
            } else {
                qrCodeContent.append("");
            }
            String fileName = QRCodeUtils.encode(qrCodeContent.toString(), PropertyUtils.getProperty("aliyun.properties", "ali.localUrl.temp"),200);
            aliUrl.append(AliOssUtils.upLoadFile(fileName));
        }
        return aliUrl.toString();
    }


}
